package com.gf.user.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.base.type.Role;
import com.gf.common.bean.Page;
import com.gf.user.dao.SuperDao;
import com.gf.user.mapper.UserMapper;
import com.gf.user.model.User;

@Service("superDao")
public class SuperDaoImpl extends BaseDaoImpl implements SuperDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * select * from user where role=?
	 */
	@Value("${superDao.queryAdminsSql}")
	private String queryAdminsSql;

	/**
	 * update user set role=? where userId=?
	 */
	@Value("${superDao.updateUserToAdminSql}")
	private String updateUserToAdminSql;

	/**
	 * select count(1) from user where role=?
	 */
	@Value("${superDao.queryAdminNumberSql}")
	private String queryAdminNumberSql;

	/**
	 * insert into about (content,createtime) values (?,current_timestamp)
	 */
	@Value("${superDao.insertAboutSql}")
	private String insertAboutSql;

	@Override
	public List<User> queryAdmins(Page page) {
		return this.getPaginationResult(queryAdminsSql, page,
				UserMapper.getMapper(), Role.ADMIN.toString());
	}

	@Override
	public boolean updateUserToAdminOrNot(long adminId, Role role) {
		int result = jdbcTemplate.update(updateUserToAdminSql, role.toString(),
				adminId);
		return result > 0 ? true : false;
	}

	@Override
	public int queryAdminsCount() {
		return jdbcTemplate.queryForInt(queryAdminNumberSql,
				Role.ADMIN.toString());
	}

	@Override
	public long insertAbout(String content) {

		return this.insertWithIdReturn(insertAboutSql, content);
	}

}
